import { LANG, TAGS_VIEW, SHOW_LOGO } from '@/constant'
import { getItem, setItem } from '@/utils/storage'
export default {
  namespaced: true,
  state: () => ({
    isFixedHeader: false, // 是否固定header
    showTagsView: true, // 是否开启tagsView
    showGlobalSetting: false, // 是否打开全局设置的抽屉
    showLogo: true, // 是否展示logo
    sidebarOpened: true, // 左侧菜单栏是否展开
    language: getItem(LANG) || 'zh', // 语言
    tagsViewList: getItem(TAGS_VIEW) || [] // tagsView缓存的数组
  }),
  mutations: {
    changeFixedHeader(state, payload) {
      state.isFixedHeader = payload
    },
    changeShowTagsView(state, payload) {
      state.showTagsView = payload
    },
    changeShowGlobalSetting(state, payload) {
      state.showGlobalSetting = payload
    },
    changeShowLogo(state, payload) {
      state.showLogo = payload
      setItem(SHOW_LOGO, state.showLogo)
    },
    // 移除tagsView
    removeTagsView(state, payload) {
      if (payload.type === 'index') {
        state.tagsViewList.splice(payload.index, 1)
      } else if (payload.type === 'other') {
        state.tagsViewList.splice(
          payload.index + 1,
          state.tagsViewList.length - payload.index + 1
        )
        state.tagsViewList.splice(0, payload.index)
      } else if (payload.type === 'right') {
        state.tagsViewList.splice(
          payload.index + 1,
          state.tagsViewList.length - payload.index + 1
        )
      }
      setItem(TAGS_VIEW, state.tagsViewList)
    },
    // 修改指定的tag为 title
    changeTagsView(state, { index, tag }) {
      state.tagsViewList[index] = tag
      setItem(TAGS_VIEW, state.tagsViewList)
    },
    // 添加tags
    addTagsViewList(state, tag) {
      // 看看是不是已经存在于我们的数组当中
      const isFind = state.tagsViewList.find((item) => {
        return item.path === tag.path
      })
      // 如果不存在，那么添加到数组当中
      if (!isFind) {
        state.tagsViewList.push(tag)
        // 缓存起来
        setItem(TAGS_VIEW, state.tagsViewList)
      }
    },
    // 控制sidebar的开关
    triggerSidebarOpened(state) {
      state.sidebarOpened = !state.sidebarOpened
    },
    // 设置语言
    setLanguage(state, lang) {
      setItem(LANG, lang)
      state.language = lang
    }
  },
  actions: {}
}
